package Shuffle;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;


/**
 * 利用Map特性來產生一副牌的大小，繼承自Poke類別
 * @author QK
 */
public class pokeTypeB extends Poke{
	private Map<String, Poke> mapCards;
	private int sufCardTime;
	
	/**
	 * 預設洗牌100次，也會產生一副牌
	 */
	public pokeTypeB(){
		setSufCardTime(100);
		mapCards = dealNewCards();
	}
	
	/**
	 * 可以指定洗牌次數
	 * @param timer
	 */
	public pokeTypeB(int timer){
		setSufCardTime(timer);
		mapCards = dealNewCards();
	}
	
	/**
	 * @return取得目前洗好的牌組
	 */
	public Map getCards(){
		
		return mapCards;
	}
	
	/**
	 * 繼承Poke，開始產生排組
	 * Key是數字代表第幾張牌，Value是牌實際的花色與點
	 * @return 回傳一個Map的牌組
	 */
	public HashMap<String, Poke> dealNewCards() {
		mapCards = new HashMap<String, Poke>();
		for (int cardOfNumer = 1; cardOfNumer <= cardOfNumber.length; cardOfNumer++) {
			for (int colorState = 0; colorState < cardOfColor.length; colorState++) {
				Poke poke = new Poke();
				poke.color = cardOfColor[colorState];	
				switch (cardOfNumer) {
					case 1:
						poke.number = "A";
						poke.cardOrder = 11;
						break;
					case 2:
						poke.number = "2";
						poke.cardOrder = 12;
						break;	
					case 11:
						poke.number = "J";
						poke.cardOrder = 8;
						break;
					case 12:
						poke.number = "Q";
						poke.cardOrder = 9;
						break;
					case 13:
						poke.number = "K";
						poke.cardOrder = 10;
						break;
					default:
						poke.number = String.valueOf(cardOfNumer);
						poke.cardOrder = cardOfNumer-1;
				}
				mapCards.put(String.valueOf(totalCards), poke);
				totalCards--;
			}
		}
		totalCards = 52;	//恢復預設
		System.out.println(totalCards);
		return (HashMap<String, Poke>) mapCards;
	}
	
	public void showPokeCards(){
		for(int count = 1 ; count < 53 ; count++){
			Poke aCard = mapCards.get(String.valueOf(count));
			System.out.print(aCard.color+aCard.number + "\t");
			if(count % 13==0)
				System.out.println();
		}
	}
	
	/**
	 * 隨機取得變數來隨意找兩張牌交換
	 */
	public void suffleCard(){
		Random ran = new Random();
		for(int time = 0 ; time < getSufCardTime() ;time ++ ){
			for(int i = 1 ; i < 53 ; i++){
				int key1 = ran.nextInt(52)+1;
				int key2 = ran.nextInt(52)+1;
				swapCard(key1,key2);
			}
		}
	}
	
	public void swapCard(int key1, int key2){
	    Poke temp = mapCards.get(String.valueOf(key1));
	    mapCards.replace(String.valueOf(key1), mapCards.get(String.valueOf(key2)));
	    mapCards.replace(String.valueOf(key2), temp);
	}


	/**
	 * 取得洗牌次數
	 * @return
	 */
	public int getSufCardTime() {
		return sufCardTime;
	}

	/**
	 * 設定洗牌次數
	 * @return
	 */
	public void setSufCardTime(int sufCardTime) {
		this.sufCardTime = sufCardTime;
	}


}
